/*
 * @(#)XButton.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.util;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Clase que desarrolla un bot�n con la est�tica com�n del entorno
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XButton extends JPanel implements ChangeListener {

	//----------------------------------------------------------------------------//
	//                           CONSTANTES PRIVADAS                              //
	//----------------------------------------------------------------------------//

	/**
	 * C�digo asociado a la clase serializable
	 */
	private static final long serialVersionUID = 95505666603000L;

	//----------------------------------------------------------------------------//
	//                            MIEMBROS PRIVADOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Bot�n
	 */
	private JButton jbutton;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor con el texto del bot�n
	 */
	public XButton(String label) {
		super();
		setLayout(new GridLayout(1,1));
		jbutton = new JButton("  "+label+"  ");
		jbutton.setBorder(BorderFactory.createRaisedBevelBorder());
		jbutton.setHorizontalAlignment(JButton.CENTER);
		jbutton.setFont(XConstants.font);
		jbutton.setFocusPainted(false);
		jbutton.setContentAreaFilled(false);
		jbutton.addChangeListener(this);
		add(jbutton);
		pack();
	}

	/**
	 * Constructor con el texto y el controlador
	 */
	public XButton(String label, ActionListener action) {
		this(label);
		if(action != null) jbutton.addActionListener(action);
	}

	/**
	 * Constructor con el texto y la anchura
	 */
	public XButton(String label, int width) {
		this(label);
		setPreferredWidth(width);
	}

	/**
	 * Constructor con el texto, el controlador y la anchura
	 */
	public XButton(String label, ActionListener action, int width) {
		this(label);
		if(action != null) jbutton.addActionListener(action);
		setPreferredWidth(width);
	}

	/**
	 * Constructor con el texto, el comando y el controlador
	 */
	public XButton(String label, String command, ActionListener action) {
		this(label);
		jbutton.setActionCommand(command);
		if(action != null) jbutton.addActionListener(action);
	}

	/**
	 * Constructor con el texto, comando, controlador y anchura
	 */
	public XButton(String label, String command, ActionListener action, int width){
		this(label);
		jbutton.setActionCommand(command);
		if(action != null) jbutton.addActionListener(action);
		setPreferredWidth(width);
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Asigna la anchura m�nima
	 */
	public void setMinimumWidth(int width) {
		Dimension minsize = new Dimension( width, getMinimumSize().height);
		setMinimumSize(minsize);
	}

	/**
	 * Asigna la anchura preferida
	 */
	public void setPreferredWidth(int width) {
		Dimension prefsize = new Dimension( width, getPreferredSize().height);
		setPreferredSize(prefsize);
	}

	/**
	 * Asigna la anchura m�xima
	 */
	public void setMaximumWidth(int width) {
		Dimension maxsize = new Dimension( width, getMaximumSize().height);
		setMaximumSize(maxsize);
	}

	/**
	 * Bloquea la altura del bot�n
	 */
	public void pack() {
		Dimension maxsize = getMaximumSize();
		Dimension prefsize = getPreferredSize();
		setMaximumSize(new Dimension(maxsize.width,prefsize.height));
	}

	/**
	 * Asigna el texto del bot�n
	 */
	public void setText(String text) {
		this.jbutton.setText(text);
	}

	/**
	 * Obtiene el texto del bot�n
	 */
	public String getText() {
		return this.jbutton.getText();
	}

	/**
	 * (Des)Habilita el bot�n
	 */
	public void setEnabled(boolean b) {
		this.jbutton.setEnabled(b);
	}

	/**
	 * Asigna la alineacion del texto
	 */
	public void setAlignment(int ag) {
		jbutton.setHorizontalAlignment(ag);
	}

	/**
	 * Asigna la fuente de letra
	 */
	public void setLabelFont(Font font) {
		jbutton.setFont(font);
	}

	/**
	 * Asigna la descripci�n del bot�n
	 */
	public void setToolTipText(String tip) {
		jbutton.setToolTipText(tip);
	}

	/**
	 * Interfaz ChangeListener
	 */
	public void stateChanged(ChangeEvent e) {
		if(jbutton.isSelected())
			jbutton.setBorder(BorderFactory.createLoweredBevelBorder());
		else jbutton.setBorder(BorderFactory.createRaisedBevelBorder());
	}
}
